package ;
import haxe.Log;
import haxe.Template;
import php.db.Object;
import php.io.File;

/**
 * ...
 * @author Axel Huizinga axel@cunity.me
 */

class Link extends Object
{
	public var label:String;
	public var id:Int;
	public var url:String;
	
	public static var manager = new php.db.Manager<Link>(Link);
	
	public function new(param:Hash<String>) 
	{
		super();
		url = param.get('url');
		label =  param.get('label');
		insert();
		//sync();
	}
	
	public function change(param:Hash<String>):String
	{
		url = param.get('url');
		label =  param.get('label');	
		update();
		return displayAll(UserType.admin);
	}
	
	public static function displayAll(uType:UserType):String
	{
		var links:List<Link> = manager.objects( "SELECT * FROM Link" , uType == UserType.admin);
		var t:Template = new Template(File.getContent('site/template/links.mtt'));
		var res:String = '';
		var task:String = Main.param.get('task');
		//Main.errLog('task:' + task);
		try
		{
			res = t.execute({admin:uType == UserType.admin,  links:links, refresh:task == 'changeLink' || task == 'newLink'});
		}
		catch (ex:Dynamic)
		{
			Main.errLog(ex);
		}
		//return t.execute({ admin:uType == UserType.admin, cats:cats});
		Main.errLog(res.length);
		return res;				
	}
	
	public static function edit(eid:Int):String
	{
		var link:Link = manager.get(eid);
		var res:String = '';
		if (link != null)
		{
			var t:Template = new Template(File.getContent('site/template/editLink.mtt'));
			res = t.execute({link:link});
		}
		return res;
	}
	
}